package com.woniu.micropropertyserver.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.OwnerInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.micropropertyserver.service.IOwnerInfoService;
import com.woniu.micropropertyserver.util.PageRest;
import com.woniu.micropropertyserver.util.ResultMsg;
import com.woniu.micropropertyserver.util.UploadData;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：bb
 * @date ：Created in 2021/1/26 0026
 * @description：
 * @modified By：
 * @version: $
 */

@RequestMapping("/owner")
@RestController
public class OwnerController {

    @Resource
    private IOwnerInfoService ownerInfoServiceImpl;


    @GetMapping(value = "/all",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg  findAll(){


        List <OwnerInfoEntity > info=ownerInfoServiceImpl.findall();
        System.out.println(info);

        return  new ResultMsg(200,"操作成功",info);
    }


    @GetMapping(value = "/findByParams",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg  findByparams(PageRest rest,  OwnerInfoEntity ownerInfo ){

        System.out.println(ownerInfo);
        System.out.println(rest);
        PageInfo info=ownerInfoServiceImpl.findAllByParams(rest, ownerInfo);
        System.out.println(info);

        return  new ResultMsg(200,"操作成功",info);
    }

    @GetMapping(value = "/id",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findById(Long id){
        System.out.println(id);
        OwnerInfoEntity info = ownerInfoServiceImpl.findById(id);

        return  new ResultMsg(200,"操作成功",info);
    }

    @GetMapping(value = "/idh",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findHouseById(Long id){
        System.out.println(id);
        OwnerInfoEntity info = ownerInfoServiceImpl.findHouseById(id);

        return  new ResultMsg(200,"操作成功",info);
    }

    @GetMapping(value = "/name",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findByName(String  name){
        System.out.println(name);
        List<OwnerInfoEntity> info = ownerInfoServiceImpl.findByName(name);

        return  new ResultMsg(200,"操作成功",info);
    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResultMsg update(@RequestBody  OwnerInfoEntity ownerInfo){
        System.out.println(ownerInfo);
        System.out.println();
        int res = ownerInfoServiceImpl.updateInfo(ownerInfo);
        if(res >0){

            return  new ResultMsg(200,"操作成功",null);
        }else{
            return  new ResultMsg(200,"更新失败",null);
        }

    }
    @PutMapping(value = "cancel",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg cancelInfo(Long id){
        int res = ownerInfoServiceImpl.delInfo(id);
        if(res >0){
            return  new ResultMsg(200,"操作成功",null);

        }else{
            return  new ResultMsg(200,"删除失败",null);
        }

    }
   @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg save(@RequestBody OwnerInfoEntity ownerInfo ){
        System.out.println(ownerInfo);

        Long res= ownerInfoServiceImpl.saveInfo(ownerInfo);

        if(res >0){

           return  new ResultMsg(200,"操作成功",null);

        }else{
            return  new ResultMsg(200,"删除失败",null);
        }
    }


    /**
     * 根据 房间id，查询它的业主
     * @param sid 房间id
     * @return
     */
    @GetMapping(value = "/findOwerBySid", produces = {"application/json;charset=utf-8"})
    public ResponseMsg findOwerBySid(Long sid){
        System.out.println("sid="+sid);
        OwnerInfoEntity ownerInfoEntity= ownerInfoServiceImpl.findOwerBySid(sid);
        return new ResponseMsg(200, "查询成功", ownerInfoEntity);
    }

    /**
     * 根据 房间id，查询它的业主id
     * @param sid
     * @return
     */
    @GetMapping(value = "/findOwerIdBySid", produces = {"application/json;charset=utf-8"})
    public ResponseMsg findOwerIdBySid(Long sid){
        System.out.println("sid="+sid);
        Long owerId= ownerInfoServiceImpl.findOwerIdBySid(sid);
        return new ResponseMsg(200, "查询成功", owerId);
    }

    /**
     * 根据 业主id，查询业主名字
     * @param id
     * @return
     */
    @GetMapping(value = "/findOwerNameById", produces = {"application/json;charset=utf-8"})
    public ResponseMsg findOwerNameById(Long id){
        System.out.println("id="+id);
        String owername= ownerInfoServiceImpl.findOwerNameById(id);
        return new ResponseMsg(200, "查询成功", owername);
    }

    @PostMapping("/upload")
    public ResultMsg upload( MultipartFile file) throws Exception {

        if (file.isEmpty()) {
            System.out.println("上传失败");
            throw new Exception("上传文件不能为空");
        }
        String url=UploadData.getUploadImgUrl(file);

        return new ResultMsg(200,"操作成功",url);
    }



}